﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using SSP;

namespace SSPDemo
{
    class MainMenuScene : SSPScene
    {
        SSPSprite background;
        SSPMenuItem startGame;
        SSPMenuItem credits;
        SSPMenuItem quit;

        int menuState; // 0 = startGame, 1 = Credits, 2 = quit

        public MainMenuScene()
        {
            isInputEnabled = true;
            background = new SSPSprite("menuscreen");
            background.position = Vector2.Divide(SSPDirector.Instance.windowSize(), 2);
            this.addChild(background);

            startGame = new SSPMenuItem("MenuStart", 421, 237);
            startGame.position = new Vector2(280, 300);
            this.addChild(startGame);

            credits = new SSPMenuItem("MenuCredit", 421, 237);
            credits.position = new Vector2(280, 400);
            this.addChild(credits);

            quit = new SSPMenuItem("MenuQuit", 421, 237);
            quit.position = new Vector2(280, 500);
            this.addChild(quit);

            this.startSelected();
            menuState = 0;
        }

        public override void update()
        {
            switch (menuState)
            {
                case 0:
                    {
                        this.startSelected();
                        break;
                    }
                case 1:
                    {
                        this.creditsSelected();
                        break;
                    }
                case 2:
                    {
                        this.quitSelected();
                        break;
                    }
                default:
                    {
                        break;
                    }
            }

            base.update();
        }

        public override void DoPlayerAction(PlayerAction action)
        {
            switch (action)
            {
                case PlayerAction.Up:
                    {
                        //Console.WriteLine("up");
                        menuState--;
                        if (menuState < 0)
                        {
                            menuState = 2;
                        }
                        break;
                    }
                case PlayerAction.Down:
                    {
                        //Console.WriteLine("down");
                        menuState++;
                        if (menuState > 2)
                        {
                            menuState = 0;
                        }
                        break;
                    }
                case PlayerAction.Jump:
                    {
                        //Console.WriteLine("hit");
                        this.runSelection();
                        break;
                    }
                default:
                    {
                        break;
                    }
            }
        }

        public void startSelected()
        {
            startGame.selected();
            credits.unselected();
            quit.unselected();
        }

        public void quitSelected()
        {
            startGame.unselected();
            credits.unselected();
            quit.selected();
        }

        public void creditsSelected()
        {
            startGame.unselected();
            credits.selected();
            quit.unselected();
        }

        public void runSelection()
        {
            switch (menuState)
            {
                case 0:
                    {
                        SSPDirector.Instance.replaceScene(new OpeningMovie());
                        break;
                    }
                case 1:
                    {
                        SSPDirector.Instance.replaceScene(new Credits());
                        break;
                    }
                case 2:
                    {
                        SSPDirector.Instance.gameInstance.Exit();
                        break;
                    }
                default:
                    {
                        break;
                    }
            }
        }

    }
}
